cmake_minimum_required(VERSION 3.16)
project(publish_pkg)  # 要是包名称

message("============== 构建项目开始 ==============")

# ------- all_in_one beg -------

# 判断当前目录是否为 cmake的根目录,如果不是,则是被其他项目引用的,不需要编译测试,工具模块
message("AO当前文件根目录 (CMAKE_CURRENT_SOURCE_DIR) ${CMAKE_CURRENT_SOURCE_DIR}")
message("AO当前项目根目录 (CMAKE_SOURCE_DIR) ${CMAKE_SOURCE_DIR}")

# 当前文件路径
set(AO_FILE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
# 项目根CMakeLists.txt文件路径
set(AO_PROJ_ROOT "${CMAKE_SOURCE_DIR}")
set(CMAKE_MODULE_PATH "${AO_FILE_ROOT}/cmake;${CMAKE_MODULE_PATH}")
if("${AO_FILE_ROOT}" STREQUAL "${AO_PROJ_ROOT}")
	set(BUILD_AS_PROJ TRUE)
	message("作为单个工程编译")
	include(${AO_PROJ_ROOT}/cmake/ThirdSupport.cmake)
else ()
	set(BUILD_AS_LIB TRUE)
	message("作为嵌入库编译")
endif()

include(ThirdSupport)
include(all_in_one)

# 设置默认程序根目录,程序中所有路径均参照此路径,方便调试和发布
if(DEBUG_MODE)
    set(DEFAULT_CONFIG_DIR "${CMAKE_SOURCE_DIR}/config")
    add_definitions("-DDEFAULT_ROOT_DIR=\"${CMAKE_SOURCE_DIR}\"")
    add_definitions("-DDEFAULT_CONFIG_DIR=\"${DEFAULT_CONFIG_DIR}\"")
else()
    add_definitions("-DDEFAULT_ROOT_DIR=\"./\"")
    add_definitions("-DDEFAULT_CONFIG_DIR=\"./config\"")
endif()
message("当前根目录: ${DEFAULT_ROOT_DIR}")
message("配置文件目录: ${DEFAULT_CONFIG_DIR}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core)
add_subdirectory(core)  # core代码

# ------- all_in_one end -------

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rclcpp REQUIRED)

foreach(dirname ${CMAKE_CURRENT_SOURCE_DIR}/src)
    file(GLOB_RECURSE SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
            "${dirname}/*.h"
            "${dirname}/*.hpp"
            "${dirname}/*.c"
            "${dirname}/*.cpp"
            )
endforeach()

add_executable(publishNode ${SRC})

target_include_directories(publishNode PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

target_compile_features(publishNode PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17
ament_target_dependencies(
  publishNode
  "std_msgs"
  "rclcpp"
)

# ------- 链接 自定义库 beg -------
target_link_libraries(publishNode core_lib)
# ------- 链接 自定义库 end -------

install(TARGETS publishNode
  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
